package com.yycx.module.bus.provider.handler.haiying.tasktodo.settle.invoice;

import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import cn.hutool.core.date.DateUtil;
import com.yycx.bpm.provider.common.CommentTypeEnum;
import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.mybatis.interceptor.ExportInterceptor;
import com.yycx.common.mybatis.interceptor.PageInterceptor;
import com.yycx.common.mybatis.model.PageParams;
import com.yycx.common.mybatis.query.CriteriaQuery;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.utils.ApiAssert;
import com.yycx.common.utils.ConvertUtils;
import com.yycx.module.admin.client.entity.BaseUser;
import com.yycx.module.admin.provider.service.BaseRoleService;
import com.yycx.module.admin.provider.service.BaseUserService;
import com.yycx.module.bus.client.entity.*;
import com.yycx.module.bus.client.entity.hy.BusHyorderInoviceDetails;
import com.yycx.module.bus.client.entity.hy.BusHyorderReturn;
import com.yycx.module.bus.client.entity.hy.HyOrder;
import org.flowable.engine.TaskService;
import org.flowable.engine.task.Comment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 开票单明细
 *
 * @author zyf
 */
@Component
public class HyInvoiceDetailsPageListHandler implements PageInterceptor<BusHyorderInoviceDetails>, ExportInterceptor {

    @Autowired
    private HttpServletRequest request;
    @Autowired
    private TaskService taskService;
    @Autowired
    private BaseUserService userService;
    @Autowired
    private BaseRoleService roleService;


    @Override
    public void prepare(CriteriaQuery<BusHyorderInoviceDetails> cq, PageParams pageParams, EntityMap params) {
        cq.clear();
        Long settleInvoiceId = params.getLong("settleInvoiceId");
        ApiAssert.isNotEmpty("参数异常", settleInvoiceId);
        String beginDate = params.get("beginDate");
        String endDate = params.get("endDate");
        String orderNo = params.get("orderNo");
        String saleManName = params.get("saleManName");
        cq.select(BusHyorderInoviceDetails.class, "*");
        cq.addSelect(HyOrder.class, "orderId", "outOrderNo", "orderNo", "sendDate", "businessCode", "sendType", "customBondType", "saleManName");
        cq.select(BusHyorderReturn.class, "drugsName", "drugsSpec", "drugType", "salePrice", "saleNum", "saleAmount","settleAmount", "returnAmount", "returnDate");
        cq.select(BusCustom.class, "customName", "settleType");
        cq.select(BusDept.class, "deptName");
        //业务员
        cq.like("ho.saleManName", saleManName);
        //发货单号
        if (FlymeUtils.isNotEmpty(orderNo)) {
            cq.and(e -> e.like("ho.orderNo", orderNo).or().like("ho.outOrderNo", orderNo));
        }
        //购货方
        cq.like(BusCustom.class, "customName");
        //发货日期
        cq.ge(FlymeUtils.isNotEmpty(beginDate), "ho.sendDate", beginDate);
        cq.le(FlymeUtils.isNotEmpty(endDate), "ho.sendDate", endDate);
        cq.eq(true, "bhid.settleInvoiceId", settleInvoiceId);
        cq.eq(HyOrder.class, "deptId");
        cq.createJoin(BusCustom.class);
        cq.createJoin(BusHyorderReturn.class).setMainField("orderDetailsReturnId").setJoinField("orderDetailsReturnId");
        cq.createJoin(BusDept.class);
        cq.createJoin(HyOrder.class);

    }

    @Override
    public List<EntityMap> complete(CriteriaQuery<BusHyorderInoviceDetails> cq, List<EntityMap> result, EntityMap extra) {
        for (EntityMap entityMap : result) {
            Long settleFpId = entityMap.getLong("settleFpId");
            entityMap.put("disabled", false);
            if (FlymeUtils.isNotEmpty(settleFpId)) {
                entityMap.put("disabled", true);
            }
        }
        return PageInterceptor.super.complete(cq, result, extra);
    }

    @Override
    public void initExportData(Map dataMap, List list,CriteriaQuery cq) {
        dataMap.put("nickName", OpenHelper.getNickName());
        dataMap.put("applyDate", DateUtil.format(DateUtil.date(), "yyyy-MM-dd"));
        String processInstanceId = request.getParameter("processInstanceId");
        //查询审批人
        if (FlymeUtils.isNotEmpty(processInstanceId)) {
            List<Comment> commentsList = taskService.getProcessInstanceComments(processInstanceId, CommentTypeEnum.WC.toString());
            if (FlymeUtils.isNotEmpty(list)) {
                commentsList = commentsList.stream().filter(ConvertUtils.distinctByKey(Comment::getUserId)).collect(Collectors.toList());
                for (Comment comment : commentsList) {
                    String userId = comment.getUserId();
                    BaseUser baseUser = userService.getUserById(Long.parseLong(userId));
                    List<String> roleCodes = roleService.selectRoleCodesByUserId(baseUser.getUserId());
                    if (roleCodes.contains("ROLE_CW_JSY")) {
                        dataMap.put("nickName_cw_jsy", baseUser.getNickName());
                    }
                    if (roleCodes.contains("ROLE_CW_FHY")) {
                        dataMap.put("nickName_cw_fhy", baseUser.getNickName());
                    }
                    if (roleCodes.contains("ROLE_SW_SCZZ")) {
                        dataMap.put("nickName_sw_sczz", baseUser.getNickName());
                    }
                    if (roleCodes.contains("ROLE_SW_FHY")) {
                        dataMap.put("nickName_sw_fhy", baseUser.getNickName());
                    }
                    if (roleCodes.contains("ROLE_CW_JSBX")) {
                        dataMap.put("nickName_jsbz", baseUser.getNickName());
                    }
                    if (roleCodes.contains("ROLE_CW_CWBZ")) {
                        dataMap.put("nickName_cwbz", baseUser.getNickName());
                    }
                    if (roleCodes.contains("ROLE_CW_ZJ")) {
                        dataMap.put("nickName_cwzj", baseUser.getNickName());
                    }
                    if (roleCodes.contains("ROLE_FLP_ZJL")) {
                        dataMap.put("nickName_zjl", baseUser.getNickName());
                    }
                }
            }
        }
    }
}
